package com.me.Inheritor;

public class Platform extends ControllerObject
{
	float scrollSpeed;
	
	public Platform()
	{
		scrollSpeed = 200;
		setTexture( GameData.getTextureManager().LoadTexture( "data/platform.png" ) );
	}
	
	public void Update( float dt )
	{
		prevPosition.x = position.x;
		prevPosition.y = position.y;
		
		position.x -= scrollSpeed * dt;
		
		if( position.x + width < 0 )
			GameData.getObjectManager().RemoveObject( this );
		
		// Collision detection against the player
		Player player = GameData.getPlayer();
		
		float pWidth = player.getWidth();
		float pHeight = player.getHeight();
		float pX = player.getX();
		float pY = player.getY();
		
		// Colliding on the X-Axis?
		if( pX > position.x + width || pX + pWidth < position.x )
			return;
		
		// Colliding on the Y-Axis?
		if( pY > position.y + height || pY + pHeight < position.y )
			return;

		// There has been a collision, now fix it.
		
		player.setYVelocity( 0 );
		
		// Is the player about to land on the platform?
		if( player.prevPosition.y >= position.y+height )
		{
			player.setY( position.y + height );
		}
		else
		{
			player.setY( position.y - pHeight );
		}
	}
}
